import { createStore } from "vuex";

export default createStore({
  state: {
    list:JSON.parse(sessionStorage.getItem("todos"))||[],
  },
  mutations: {
    addTodo(state, payload) {
      state.list.push(payload);
    },
    delTodo(state, index) {
      state.list.splice(index, 1);
    },
    clear(state) {
      state.list = state.list.filter((item) => !item.done);
    },
    checkTodo(state, index) {
      console.log(state.list[index]);
      let obj = state.list[index];
      obj.done = !obj.done;
    },
    checkAllTodo(state, value) {
      state.list.forEach((item) => (item.done = value));
    },
    updateTodo(state, { index, title }) {
      console.log(index, title);
      state.list[index].title = title;
    },
  },
});
